package test2;
public class ArrayList {
    public static void main(String[] args) {
        //1.创建集合
        java.util.ArrayList<User> list = new java.util.ArrayList<User>();
        //2.创建三个用户对象
        User u1 = new User("zhansan",18);
        User u2 = new User("lisi",19);
        User u3 = new User("wangwu",20);
        //3.添加元素
         list.add(u1);
         list.add(u2);
         list.add(u3);
         //判断lisi 19 在集合是否存在,并返回对应下标
          User userInfo  =new User("wangwu",20);
         int index= cheakuserInfo(list,userInfo);
         if(index==-1){
             System.out.println("不存在");
         }else{
             User u = list.get(index);
             System.out.println("存在下标为:"+index);
             System.out.println(u.getName()+" "+u.getAge());
         }


    }

    private static int cheakuserInfo(java.util.ArrayList<User> list, User userInfo) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if(u.getName().equals(userInfo.getName())&&u.getAge()==userInfo.getAge()){
                return i;
            }
        }
        return -1;
    }
}
